//
//  NewVersionAlertView.swift
//  readtool
//
//  Created by Wan Jiading on 2016/11/3.
//  Copyright © 2016年 DAD. All rights reserved.
//	新版本提示页

import UIKit

protocol NewVersionAlertViewDelegate: NSObjectProtocol {
	func newVersionAlertView(view: NewVersionAlertView, clickedButtonAtIndex: Int)
}

class NewVersionAlertView: UIView {
	
	var delegate: NewVersionAlertViewDelegate?
	
	var backgroundView = UIView()
	var imageView = UIImageView()
	var titleLabel = UILabel()
	var contentLabel = UILabel()
	var btnView = UIView()
	var cancelButton = UIButton()
	
	var maskButton = UIButton()
	
	let ScreenWidth = (MainScreen.bounds.size.width)
	let ScreenHeight = (MainScreen.bounds.size.height)
	let ContentButtonVerticalMargin:CGFloat = 20        // content中按钮 距离顶部 和 底部的 距离
	let ContentButtonHeight:CGFloat = 76
	
	var backgroundWidth:CGFloat {
		if IPHONE5 || IPHONE4 {
			return MainScreen.bounds.size.width * 0.9
		}
		return MainScreen.bounds.size.width * 0.76
	}
	let backgroundMaxHeight: CGFloat = MainScreen.bounds.size.height * 0.9
	var btnViewTop: CGFloat = 30 {
		didSet {
			layoutIfNeeded()
		}
	}
	
	convenience init(_ imageName: String, _ title: String, _ content: String, _ buttonTitles: [String], delegate: NewVersionAlertViewDelegate?) {
		self.init()
		self.delegate = delegate
		
		// 主题内容背景view
		let backgroundView: UIView = UIView()
		backgroundView.backgroundColor = UIColor.white
		backgroundView.width = backgroundWidth
		self.backgroundView = backgroundView
		
		// 背景按钮
		let maskButton: UIButton = UIButton(type: UIButtonType.custom)
		maskButton.backgroundColor = UIColor.rgb("000000", alpha: 0.5)
		maskButton.frame = MainScreen.bounds
		maskButton.addSubview(backgroundView)
		maskButton.addTarget(self, action: #selector(dismissSelf), for: UIControlEvents.touchUpInside)
		self.maskButton = maskButton
		self.addSubview(maskButton)
		self.frame = maskButton.bounds

		// imageView
		let imageView = UIImageView()
		imageView.image = UIImage(named: imageName)
		imageView.sizeToFit()
		backgroundView.addSubview(imageView)
		self.imageView = imageView
		
		// 标题
		let titleLabel = UILabel()
		titleLabel.font = UIFont.systemFont(ofSize: 16)
		titleLabel.text = title
		titleLabel.textColor = UIColor.rgb("000000")
		titleLabel.numberOfLines = 0
		titleLabel.width = backgroundView.width - 24 * 2
		titleLabel.sizeToFit()
		titleLabel.textAlignment = NSTextAlignment.center
		backgroundView.addSubview(titleLabel)
		self.titleLabel = titleLabel
		
		// 内容
		let contentLabel = UILabel()
		contentLabel.font = UIFont.systemFont(ofSize: 14)
		contentLabel.text = content
		contentLabel.textColor = UIColor.rgb("666666")
		contentLabel.numberOfLines = 0
		contentLabel.width = backgroundView.width - 24 * 2
		contentLabel.setLinespace(6)
		contentLabel.sizeToFit()
		backgroundView.addSubview(contentLabel)
		self.contentLabel = contentLabel
		
		// 按钮
		let btnViewY: CGFloat 		= btnViewTop + contentLabel.frame.maxY
		let btnWidth: CGFloat 		= 110
		let btnHeight: CGFloat 		= 36
		let btnMargin: CGFloat 		= 34
		
		let btnCount = buttonTitles.count
		let leftMargin: CGFloat		= (backgroundView.width - btnWidth * CGFloat(btnCount) - btnMargin * CGFloat((btnCount - 1))) * 0.5
		
		for i in 0..<btnCount {
			
			let btn = UIButton(title: buttonTitles[i], titleColor: UIColor.rgb("84a740"), backgroundColor: UIColor.white, fontSize: 14, target: self, action: #selector(btnClick(_:)))
			btn.tag = 1000 + i
			let x: CGFloat = leftMargin + (btnWidth + btnMargin) * CGFloat(i)
			btn.frame = CGRect(x: x, y: 0, width: btnWidth, height: btnHeight)
			btn.layer.borderColor = UIColor.rgb("9bc44d").cgColor
			
			if i == btnCount - 1 {
				btn.setTitleColor(UIColor.white, for: .normal)
				btn.backgroundColor = UIColor.rgb("9bc44d")
				btn.layer.borderColor = btn.backgroundColor!.cgColor
			}
			
			btn.layer.cornerRadius = 5
			btn.layer.borderWidth = GlobalConst.DAD2PX
			
			btnView.addSubview(btn)
		}
		btnView.frame = CGRect(x: 0, y: btnViewY, width: backgroundView.width , height: btnHeight)
		backgroundView.addSubview(btnView)
		
		// 设置最终的frame
		backgroundView.layer.cornerRadius = 10
		
		self.addSubview(maskButton)
		self.frame = maskButton.bounds
		maskButton.isUserInteractionEnabled = true
		self.isUserInteractionEnabled = true
		SharedApplication.keyWindow!.addSubview(self)
	}
	
	override func layoutSubviews() {
		super.layoutSubviews()
		
        //新增一行代码 处理图片过宽
        imageView.width = backgroundView.width
		imageView.y = 0
		imageView.x = (backgroundView.width - imageView.width) * 0.5
		        
		titleLabel.y = imageView.frame.maxY + 20
		titleLabel.x = (backgroundView.width - titleLabel.width) * 0.5
		
		contentLabel.y = titleLabel.frame.maxY + 24
		contentLabel.x = 24
		
		btnView.y = btnViewTop + contentLabel.frame.maxY
		let currentHeight = backgroundView.subviews.last!.frame.maxY + 30
		if currentHeight > backgroundMaxHeight {
			
			contentLabel.height = contentLabel.height - (currentHeight - backgroundMaxHeight)
			
			let fontSize = contentLabel.font.pointSize
			contentLabel.font = UIFont.systemFont(ofSize: (fontSize - 1))
			contentLabel.sizeToFit()
			
			layoutSubviews()
		}
		
		backgroundView.height = backgroundView.subviews.last!.frame.maxY + 30
		backgroundView.x = (ScreenWidth - backgroundWidth) * 0.5
		backgroundView.y = (ScreenHeight - backgroundView.height) * 0.5
	}
	
	@objc private func btnClick(_ btn: UIButton) {
		if delegate != nil {
			delegate!.newVersionAlertView(view: self, clickedButtonAtIndex: btn.tag - 1000)
		}
	}
	
	public func show() {
		let animation = CAKeyframeAnimation(keyPath: "transform")
		animation.duration = 0.25
		
		let values = [
			NSValue.init(caTransform3D: CATransform3DMakeScale(0.8, 0.8, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(1.01, 1.01, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(0.99, 0.99, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
			]
		animation.values = values
		backgroundView.layer.add(animation, forKey: nil)
		
		maskButton.backgroundColor = UIColor.rgb("000000", alpha: 0.5)
		UIApplication.shared.keyWindow?.addSubview(self)
	}
	
	
	func dismissSelf() {
		let animation = CAKeyframeAnimation(keyPath: "transform")
		animation.duration = 0.25
		let values = [
			NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(0.8, 0.8, 1.0)),
			NSValue.init(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)),
			]
		animation.values = values
		backgroundView.layer.add(animation, forKey: nil)
		
		UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: { [weak self] in
			self?.maskButton.alpha = 0
		}, completion:{ [weak self] (finished) in
			self?.removeFromSuperview()
		})
	}
	
    ///取消黑色背景点击失效
    func maskButtonRemoveTarget() {
        maskButton.removeTarget(self, action: #selector(dismissSelf), for: .touchUpInside)
    }
    
	deinit {
		DebugLogFunc()
	}
}
